package com.atguigu.edustatistics.service.impl;

import com.atguigu.commonutils.Result;
import com.atguigu.edustatistics.client.UcenterClient;
import com.atguigu.edustatistics.entity.StatisticsDaily;
import com.atguigu.edustatistics.mapper.StatisticsDailyMapper;
import com.atguigu.edustatistics.service.StatisticsDailyService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author 董万均
 * @since 2020-11-08
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public void registerCount(String day) {
        //先删除表中相同的天数的记录
        QueryWrapper<StatisticsDaily> wrapper=new QueryWrapper<>();
        wrapper.eq("date_calculated",day);
        baseMapper.delete(wrapper);

        Result result = ucenterClient.registerCount(day);
        Integer registerCount = (Integer) result.getData().get("registerCount");
        //将数据封装到对象
        StatisticsDaily statisticsDaily=new StatisticsDaily();
        statisticsDaily.setDateCalculated(day);
        statisticsDaily.setRegisterNum(registerCount);
        statisticsDaily.setCourseNum(RandomUtils.nextInt(100,200));
        statisticsDaily.setLoginNum(RandomUtils.nextInt(100,200));
        statisticsDaily.setVideoViewNum(RandomUtils.nextInt(100,200));
        baseMapper.insert(statisticsDaily);

    }

    //显示图表
    @Override
    public Map<String, Object> showChart(String type, String begin, String end) {
        //查询图表
        QueryWrapper<StatisticsDaily> wrapper=new QueryWrapper<>();
        wrapper.between("date_calculated",begin,end);
        //只想返回需要的结果，只需要日期和类型的数据
        wrapper.select("date_calculated",type);
        List<StatisticsDaily> selectList = baseMapper.selectList(wrapper);

        //因为返回的有两部分数据：日期和日期对应的数量
        //前端需要返回数组json结果，对应后端java代码中的list集合
        //创建两个list集合，一个日期list，一个数量list集合
        List<String> dateList=new ArrayList<>();
        List<Integer> numberList=new ArrayList<>();

        //遍历所有的数据，进行封装
        for (int i = 0; i < selectList.size(); i++) {
            StatisticsDaily daily = selectList.get(i);
            //封装日期
            dateList.add(daily.getDateCalculated());
            //封装日期对应的数据
            switch (type){
                case "login_num":
                    numberList.add(daily.getLoginNum());
                    break;
                case "register_num":
                    numberList.add(daily.getRegisterNum());
                    break;
                case "video_view_num":
                    numberList.add(daily.getVideoViewNum());
                    break;
                case "course_num":
                    numberList.add(daily.getCourseNum());
                    break;
                default:
                    break;
            }

        }

        Map<String,Object> map=new HashMap<>();
        map.put("dateList",dateList);
        map.put("numDataList",numberList);
        return map;
    }
}
